<template>
  <div class="register-container">
    <div class="register-form-container">
      <div class="register-header">
        <img src="https://img.icons8.com/color/96/000000/heart-health.png" alt="康养中心" class="logo" />
        <h1>康养中心患者监护系统--医生端</h1>
        <p>创建您的账号，开始使用专业的康养服务</p>
      </div>
      
      <el-form ref="formRef" :model="registerForm" :rules="registerRules" class="register-form" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="registerForm.username" 
            :prefix-icon="User"
            placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        
        <el-form-item label="姓名" prop="name">
          <el-input 
            v-model="registerForm.name" 
            :prefix-icon="UserFilled"
            placeholder="请输入真实姓名">
          </el-input>
        </el-form-item>
        
        <el-form-item label="专业领域" prop="specialty">
          <el-input 
            v-model="registerForm.specialty" 
            :prefix-icon="Briefcase"
            placeholder="请输入您的专业领域">
          </el-input>
        </el-form-item>
        
        <el-form-item label="联系方式" prop="contact">
          <el-input 
            v-model="registerForm.contact" 
            :prefix-icon="Phone"
            placeholder="请输入联系方式">
          </el-input>
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="registerForm.password" 
            :prefix-icon="Lock"
            type="password" 
            placeholder="请输入密码"
            show-password>
          </el-input>
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword" 
            :prefix-icon="Lock"
            type="password" 
            placeholder="请再次输入密码"
            show-password>
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-checkbox v-model="agreeTerms">我已阅读并同意<el-link type="primary" underline="never">服务条款</el-link>和<el-link type="primary" underline="never">隐私政策</el-link></el-checkbox>
        </el-form-item>
        
        <el-button type="primary" class="register-button" @click="handleRegister" :loading="loading" :disabled="!agreeTerms">注册</el-button>
        
        <div class="login-link">
          已有账号? <el-link type="primary" @click="$router.push('/login')" underline="never">立即登录</el-link>
        </div>
      </el-form>
    </div>
    
    <div class="register-decoration">
      <div class="decoration-overlay">
        <h2>专业呵护，健康守护</h2>
        <p>我们的专业团队将为您提供全方位的健康管理服务</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, UserFilled, Lock, Briefcase, Phone } from '@element-plus/icons-vue'
import { addDoctors } from '@/api/doctors'

const router = useRouter()

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (registerForm.confirmPassword !== '') {
      formRef.value.validateField('confirmPassword')
    }
    callback()
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const registerForm = reactive({
  username: '',
  name: '',
  specialty: '',
  contact: '',
  password: '',
  confirmPassword: ''
})

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  specialty: [
    { required: true, message: '请输入专业领域', trigger: 'blur' }
  ],
  contact: [
    { required: true, message: '请输入联系方式', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validatePass2, trigger: 'blur' }
  ]
}

const loading = ref(false)
const agreeTerms = ref(false)
const formRef = ref(null)

const handleRegister = () => {
  if (!agreeTerms.value) {
    ElMessage.warning('请先阅读并同意服务条款和隐私政策')
    return
  }
  
  formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      
      try {
        // 调用addDoctors API注册医生账号
        // 确保字段与doctors表一致: id, name, specialty, contact, username, password
        const doctorData = {
          username: registerForm.username,
          name: registerForm.name,
          specialty: registerForm.specialty,
          contact: registerForm.contact,
          password: registerForm.password
        }
        
        await addDoctors(doctorData)
        
        // 注册成功
        ElMessage.success('注册成功，请登录')
        router.push('/login')
      } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error('注册失败，请稍后再试')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.register-container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.register-form-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background-color: #fff;
  overflow-y: auto;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-header h1 {
  font-size: 24px;
  color: #40ff59;
  margin: 16px 0 8px;
}

.register-header p {
  color: #909399;
  font-size: 14px;
}

.logo {
  width: 80px;
  height: 80px;
}

.register-form {
  width: 100%;
  max-width: 400px;
}

.register-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  margin: 10px 0 20px;
}

.login-link {
  text-align: center;
  font-size: 14px;
  color: #606266;
  margin-bottom: 20px;
}

.register-decoration {
  flex: 1.2;
  position: relative;
  overflow: hidden;
  display: none;
  background: url('https://images.unsplash.com/photo-1576091160550-2173dba999ef?ixlib=rb-4.0.3&auto=format&fit=crop&w=800&q=80') center center no-repeat;
  background-size: cover;
}

@media (min-width: 992px) {
  .register-decoration {
    display: block;
  }
}

.decoration-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.6));
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  padding: 40px;
  text-align: center;
}

.decoration-overlay h2 {
  font-size: 32px;
  margin-bottom: 20px;
  font-weight: 500;
}

.decoration-overlay p {
  font-size: 18px;
  max-width: 400px;
  line-height: 1.6;
}
</style> 
